package com.example.petcare.mapper;

import com.example.petcare.pojo.Goods;
import com.example.petcare.pojo.HostDemand;
import com.example.petcare.pojo.Provider;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface HostMapper {

    // 新增托管需求
    @Insert("insert into host_demand(pet_id,user_id,days,start_date,location,price,remarks,status,order_status,reject_status,pay_status) " +
            "values(#{petId},#{userId},#{days},#{startDate},#{location},#{price},#{remarks},#{status},#{orderStatus},#{rejectStatus},#{payStatus})")
    void add(HostDemand hostDemand);


    // 查询托管需求列表(条件查询)
    List<HostDemand> list(Integer userId, Integer petId, String location, Integer status);

    // 修改托管需求
    @Update("update host_demand set pet_id=#{petId},days=#{days},start_date=#{startDate},location=#{location},price=#{price},remarks=#{remarks} ,provider_id=#{providerId}  where host_id=#{hostId}")
    void update(HostDemand hostDemand);

    // 查询委托需求详情
    @Select("select * from host_demand where host_id = #{hostId}")
    HostDemand findById(Integer hostId);

    // 删除委托需求
    @Delete("delete from host_demand where host_id = #{hostId}")
    void delete(Integer hostId);


    // 根据petId查询委托需求列表
    @Select("select * from host_demand where pet_id = #{petId}")
    List<HostDemand> findByPetId(Integer petId);

    // 查询所有已经审核过的商家的具体信息，从provider表中查询
    @Select("select * from provider where provider_status = 1")
    List<Provider> listProvider();


    // 删除已经被拒单的托管需求，利用userId删除，userId从threadLocal中获取
    @Delete("delete from host_demand where user_id = #{userId} and reject_status = 1")
    void deleteByUserId(Integer userId);

    // 查询当前用户被拒单的托管需求
    @Select("select * from host_demand where user_id = #{userId} and reject_status = 1")
    List<HostDemand> listReject(Integer userId);

    // 是否有新订单被接受
    @Select("select count(*) from host_demand where user_id = #{userId} and status = 1 and order_status = 0")
    Integer hasAccept(Integer userId);

    // 是否有订单完成
    @Select("select count(*) from host_demand where user_id = #{userId} and status = 1 and order_status = 1")
    Integer hasFinish(Integer userId);

    // 更新支付状态
    @Update("update host_demand set pay_status = 1 where host_id = #{hostId}")
    void updatePayStatus(Integer hostId);

    // 查询委托金额
    @Select("select price from host_demand where host_id = #{hostId}")
    Double getHostingPrice(Integer hostId);

    // 查看增值商品列表
    @Select("select * from goods where provider_id = #{providerId} and is_on_sale = 1 and audit_status = 1")
    List<Goods> getGoodsList(Integer providerId);

    // 根据providerId查询商家信息
    @Select("select * from provider where provider_id = #{providerId}")
    Provider getProviderById(Integer providerId);
}
